package com.telit.firecommand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.xiaolyuh.annotation.CacheEvict;
import com.github.xiaolyuh.annotation.Cacheable;
import com.github.xiaolyuh.annotation.SecondaryCache;
import com.github.xiaolyuh.support.CacheMode;
import com.telit.firecommand.dao.CadPlotIconDao;
import com.telit.firecommand.entity.CadPlotIcon;
import com.telit.firecommand.service.ICadPlotIconService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 * 标绘图形 服务实现类
 * </p>
 *
 * @author yummy
 * @since 2021-07-31
 */
@Service
@Transactional
public class CadPlotIconServiceImpl extends ServiceImpl<CadPlotIconDao, CadPlotIcon> implements ICadPlotIconService {

    @Override
    @CacheEvict(value = {"firecommand:ploticon:searchNoPage",
            "firecommand:plotAction:selectByActionValue"}, key = "'icon-searchNoPage'")
    public int add(CadPlotIcon cadPlotIcon) {
        return baseMapper.insert(cadPlotIcon);
    }

    @Override
    @CacheEvict(value = {"firecommand:ploticon:searchNoPage",
            "firecommand:plotAction:selectByActionValue"}, key = "'icon-searchNoPage'")
    public int delete(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    @CacheEvict(value = {"firecommand:ploticon:searchNoPage",
            "firecommand:plotAction:selectByActionValue"}, key = "'icon-searchNoPage'")
    public List<Integer> deleteBatch(Integer[] ids) {
        List<Integer> values = new ArrayList<Integer>();
        if (ids != null && ids.length > 0) {
            for (Integer id : ids) {
                values.add(delete(id));
            }
        }
        return values;
    }

    @Override
    @CacheEvict(value = {"firecommand:ploticon:searchNoPage",
            "firecommand:plotAction:selectByActionValue"}, key = "'icon-searchNoPage'")
    public int updateData(CadPlotIcon cadPlotIcon) {
        return baseMapper.updateById(cadPlotIcon);
    }

    @Override
    public CadPlotIcon findById(Integer id) {
        return baseMapper.selectById(id);
    }


    @Override
    public IPage<CadPlotIcon> findListByPage(CadPlotIcon cadPlotIcon, Integer page, Integer pageCount) {
        IPage<CadPlotIcon> cadPlotIconPage = new Page<>(page, pageCount);
        QueryWrapper<CadPlotIcon> wrapper = new QueryWrapper<>();
        if (cadPlotIcon != null) {
            if (StringUtils.hasText(cadPlotIcon.getUrl())) {
                wrapper.eq("url", cadPlotIcon.getUrl());
            }
            if (StringUtils.hasText(cadPlotIcon.getName())) {
                wrapper.eq("name", cadPlotIcon.getName());
            }
        }
        return baseMapper.selectPage(cadPlotIconPage, wrapper);
    }

    @Override
    @Cacheable(value = "firecommand:ploticon:searchNoPage", depict = "根据条件查询联动标绘图标", key = "'icon-searchNoPage'", cacheMode = CacheMode.SECOND, secondaryCache = @SecondaryCache(expireTime = 300, preloadTime = 2, forceRefresh = true, timeUnit = TimeUnit.SECONDS))
    public List<CadPlotIcon> searchNoPage(CadPlotIcon cadPlotIcon) {
        QueryWrapper<CadPlotIcon> wrapper = new QueryWrapper<>();
        if (cadPlotIcon != null) {
            if (StringUtils.hasText(cadPlotIcon.getUrl())) {
                wrapper.eq("url", cadPlotIcon.getUrl());
            }
            if (StringUtils.hasText(cadPlotIcon.getName())) {
                wrapper.eq("name", cadPlotIcon.getName());
            }
        }
        return baseMapper.selectList(wrapper);
    }

}
